<?php

/**
 * textmodule actions.
 *
 * @package    sf_]sandbox
 * @subpackage textmodule
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class textmoduleActions extends autotextmoduleActions
{
	public function preExecute() 
	{
		//log the logout of the user. Params: $parentId, $projectId, $className, $action, $objName, $objId, $userId
	    switch(sfContext::getInstance()->getActionName()) {
	    case 'save':
			Log::logThis(-1, 0, 'textmodule', 'save', '', $this->getRequestParameter('id'), get_user_id());
			break;
	    }
	}

    public function executePage()
    {
    	$id = $this->getRequestParameter('id');
		$tree = Tree::getById($id);
        $this->forward404Unless($tree);
        $c = new Criteria();
        $c->add(TextModulePeer::ID, $tree->getObjId());
        $c->add(TextModulePeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $this->page = TextModulePeer::doSelectOne($c);
        $this->forward404Unless($this->page);
        
        $this->arFiles = Files::getByObj(MyConstants::TYPE_TEXTMODULE, $this->page->getId()); 
    }

    protected function getTextModuleOrCreate($id = 'id')
    {
        /* @var $textModule TextModule */
        $textModule = parent::getTextModuleOrCreate($id);
        $textModule->setCulture($this->getUser()->getCulture());
        return $textModule;
    }

    public function executeEdit()
    {
        if( ($uname = strip_tags($this->getRequestParameter('uname'))) )
        {
            $name = Files::getNameFromUrl($uname);
            // Force to delete from DB
            Files::deleteByUname($name, true);
        }

        if( ($pageId = $this->getRequestParameter('pageId')) )
        {
            $sLink = '&pageId=' . $pageId;
        }

        if( $this->getRequestParameter('draft') ) 
        {
            $draft = DraftPeer::retrieveByPk($this->getRequestParameter('draft'));
            $this->text_module->setText($draft->getContent());
        }
        parent::executeEdit();
        $prototypeDir = sfConfig::get('sf_prototype_web_dir');
        $this->getResponse()->addJavascript($prototypeDir.'/js/prototype');
    }

    protected function saveTextModule($text_module)
    {

        $text_module->setStatusId(MyConstants::STATUS_APPROVED);
        $text_module->save();
        $fileName = $this->getRequest()->getFileName('text_module[new_photo]');
        $filePath = $this->getRequest()->getFilePath('text_module[new_photo]');
        if( !empty($filePath) )
        {
            Files::saveTo($fileName
                        , $filePath
                        , $text_module->getId()
                        , get_user_id()
                        , MyConstants::TYPE_TEXTMODULE
                        , $this->getUser()->getCulture()
                        , MyConstants::STATUS_APPROVED
                        , $fileName
                        , '');
        }
        if( $this->getRequestParameter('idsort') )
        {
            $id_list = $this->getRequestParameter('idsort');
            $arId = explode('id[]=', $this->getRequestParameter('idsort'));
            // change priority
            $con = Propel::getConnection();
            
            for( $i = 0; $i < count($arId); $i++ )
            {
                if( $arId[$i] )
                {
                    $c1 = new Criteria();
                    $c1->add(FilesPeer::ID, (int)str_replace('&', '', $arId[$i]));
                     
                    // update set
                    $c2 = new Criteria();
                    $c2->add(FilesPeer::PRIORITY, count($arId) - $i);
                    $affectedUpdateRows = BasePeer::doUpdate($c1, $c2, $con);
                }
            }
        }
    }

    public function executeEnable()
    {
        $id = (int)$this->getRequestParameter('id');
        $obj = TextModulePeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_APPROVED);
        $obj->save();
        $this->redirect('textmodule/list?id=' . $id);
    }
    public function executeDisable()
    {
        $id = (int)$this->getRequestParameter('id');
        //debugvar('Id: ' . $id);
        $obj = TextModulePeer::retrieveByPk($id);
        $this->forward404Unless($obj);

        $obj->setStatusId(MyConstants::STATUS_DELETED);
        $obj->save();
        $this->redirect('textmodule/list?id=' . $id);
    }
}
